import time

from selenium import webdriver
from selenium.webdriver.common.by import By

from DBUtils import DBUtils


class DeleteCardOperation:
    def deletecard_success(self, driver, username, pwd):
        # 输入用户名
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(username)
        # 输入密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(pwd)
        # time.sleep(1)
        # 点击登录按钮
        driver.find_element(By.XPATH, '//button[@id="login_btn"]').click()
        time.sleep(1)
        # 点击个人中心
        driver.find_element(By.XPATH, "//*[@id='leftbaraside']/div[2]/nav/ul/li[4]/a").click()
        time.sleep(1)
        # 点击银行卡管理
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[3]').click()
        # 选择一张银行卡，点击删除
        driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div[6]/div/div/div[1]/ul/li/button[2]').click()
        time.sleep(1)
        # 点击确定按钮
        driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()

    def deletecard_cancel(self, driver, username, pwd):
        # 输入用户名
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(username)
        # 输入密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(pwd)
        # time.sleep(1)
        # 点击登录按钮
        driver.find_element(By.XPATH, '//button[@id="login_btn"]').click()
        time.sleep(1)
        # 点击个人中心
        driver.find_element(By.XPATH, "//*[@id='leftbaraside']/div[2]/nav/ul/li[4]/a").click()
        time.sleep(1)
        # 点击银行卡管理
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[3]').click()
        # 选择一张银行卡，点击删除
        driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div[4]/div/div/div[1]/ul/li/button[2]').click()
        time.sleep(1)
        # 点击取消按钮
        driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()

    def delete_success_result(self, id):
        dbutils = DBUtils()
        result = dbutils.select('select id from bankcard')
        if id in result:
            return 0
        else:
            return 1

    def delete_cancel_result(self, id):
        dbutils = DBUtils()
        result = dbutils.select('select id from bankcard')
        if id in result:
            return 1
        else:
            return 0
